import Compiler from "./Compiler";
import Dep from "./Dep";

export default class ViewModel {
    /**
     * @param {string} el
     * @param {function} data
     */
    constructor ({ el, data }) {
        this.$el = document.querySelector(el);
        this.$data = this.reactive(data());
        this.dep = new Dep();
        this.compiler = new Compiler(this)
    }

    // proxy数据劫持
    reactive(data) {
        return new Proxy(data, {
            get: (target, key) => {
                return Reflect.get(target, key);
            },
            set: (target, key, value) => {
                const res = Reflect.set(target, key, value);
                console.log('sss', this.dep);
                this.dep.notify(key);
                return res;
            }
        })
    }
}